package org.example.controller.shanjia;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.constant.JwtClaimsConstant;
import org.example.dto.ShanjiaZhxg;
import org.example.dto.Shanjiaimagexg;
import org.example.entity.Employee;
import org.example.entity.Shanjia;
import org.example.properties.JwtProperties;
import org.example.result.Result;
import org.example.service.ShanjiaService;
import org.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/shanjia/gl")
@Slf4j
@Api(tags = "商家相关接口")
public class GlController {
    @Autowired
    ShanjiaService shanjiaService;

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private ImageController image;

    /**
     * 商品登录
     * @param shanjia
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "商家登录")
    public Result<String> sjlogin(@RequestBody Shanjia shanjia) {
        log.info("商家登录：{}", shanjia);
        shanjia=shanjiaService.login(shanjia);
        if(shanjia!=null){
            //登录成功后，生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.SHANJIA_ID, shanjia.getId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getShanjiaSecretKey(),
                    jwtProperties.getShanjiaTtl(),
                    claims);
            return Result.success(token);
        }
        else {
            return Result.error("失败");
        }
    }


    /**
     * 商家注册
     * @param shanjia
     * @return
     */
    @PostMapping("/zhuche")
    @ApiOperation(value = "商家注册")
    public Result sjzhuche(@RequestBody Shanjia shanjia) {
        log.info("商家登录：{}", shanjia);
        String password=shanjia.getPassword();
        shanjia.setPassword(null);
        if(shanjiaService.login(shanjia)==null) {
            shanjia.setPassword(password);
            shanjiaService.sizhuche(shanjia);
            return Result.success();
        }
        else {
            return Result.error("失败");
        }
    }
    /**
     * 忘记密码
     * @param shanjia
     * @return
     */
    @PutMapping("/wangji")
    @ApiOperation(value = "用户忘记密码")
    public Result<Integer> wangji(@RequestBody Shanjia shanjia) {
        log.info("忘记密码：{}", shanjia);
        String password=shanjia.getPassword();
        shanjia.setPassword(null);
        if(shanjiaService.login(shanjia)!=null){
            shanjia.setPassword(password);
            shanjiaService.wangji(shanjia);

            return Result.success();
        }
        return Result.error("失败");
    }




    /**
     * 查询
     * @return
     */
    @GetMapping ("/chaxun")
    @ApiOperation(value = "查询")
    public Result<Shanjia> chaxun(@RequestParam( "username") String username) {

        log.info("查询：{}", username);

        return Result.success(shanjiaService.chaxun(username));
    }

    /**
     * 修改店铺状态
     * @return
     */
    @GetMapping ("/xg")
    @ApiOperation(value = "修改店铺状态")
    public Result xg(@RequestParam( "username") String username) {

        log.info("修改：{}", username);
        shanjiaService.xg(username);
        return Result.success();
    }

    /**
     * 修改账号
     * @return
     */
    @PostMapping ("/zhxg")
    @ApiOperation(value = "修改账户")
    public Result zhxg(@RequestBody ShanjiaZhxg shanjiaZhxg) {

        log.info("修改：{}", shanjiaZhxg);
        Shanjia shanjia=new Shanjia();
        shanjia.setUsername(shanjiaZhxg.getUsername());
        shanjia.setPassword(shanjiaZhxg.getPassword());
        if(shanjiaService.login(shanjia)!=null) {
            shanjiaService.zhxg(shanjiaZhxg);
            return Result.success();
        }else{
            return Result.error("失败");
        }
    }

    /**
     * 修改主题
     * @param file
     * @return
     */
    @PostMapping ("/zhimage")
    @ApiOperation(value = "存储主题")
    public Result<String> zhimage(@RequestParam("theme-image") MultipartFile file) {
        String name=file.getOriginalFilename();
        String newname= UUID.randomUUID().toString()+name.substring(name.lastIndexOf("."));

        log.info("修改：{}", newname);
        try {
            file.transferTo(new File("D:/wenjian/java/image/"+newname));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return Result.success(newname);

    }

    @PutMapping ("/zhimagexg")
    @ApiOperation(value = "修改主题")
    public Result zhimagexg(@RequestBody Shanjiaimagexg shanjiaimagexg) {
        shanjiaService.xgimage(shanjiaimagexg);
        return Result.success();

    }

    /**
     * 注销账号
     * @param username
     * @return
     */
    @DeleteMapping ("/zhzx")
    @ApiOperation(value = "用户查询")
    public Result<Employee> zhzx(@RequestParam( "username") String username) {

        log.info("注销：{}", username);

        shanjiaService.delete(username);
        return Result.success( );
    }

}
